﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "CoordinateInputDefinition.h"
#include "CoordinateInputEventHandler.h"
#include "ValidatorBase.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace gcmp;

gcmp::CoordinateInputDefinition::CoordinateInputDefinition(const std::wstring& id, const std::wstring& caption, LineEditInputType inputType)
    : m_isLock(false)
    , m_pEventHandler(nullptr)
    , m_isEditingFinished(false)
{
    m_opLineEditDefinition = ILineEditDefinition::Create(id, caption, inputType);
    m_pLineEditDefinitionRefer = m_opLineEditDefinition.get();
}

gcmp::CoordinateInputDefinition::CoordinateInputDefinition(OwnerPtr<ILineEditDefinition> opDefinitionImpl)
    : m_opLineEditDefinition(TransferOwnership(opDefinitionImpl))
    , m_pEventHandler(nullptr)
    , m_isEditingFinished(false)
{
    m_pLineEditDefinitionRefer = m_opLineEditDefinition.get();
}

gcmp::CoordinateInputDefinition::~CoordinateInputDefinition()
{
}

LineEditInputType gcmp::CoordinateInputDefinition::GetInputType() const
{
    return m_pLineEditDefinitionRefer->GetInputType(); 
}

std::wstring gcmp::CoordinateInputDefinition::GetText() const
{
    return m_pLineEditDefinitionRefer->GetText();
}

bool gcmp::CoordinateInputDefinition::SetText(const std::wstring& text)
{
    if (m_isLock)
    {
        return false;
    }
    return m_pLineEditDefinitionRefer->SetText(text);
}

bool gcmp::CoordinateInputDefinition::IsEditing() const 
{ 
    return m_pLineEditDefinitionRefer->IsEditing();
}

bool gcmp::CoordinateInputDefinition::IsEditingFinished() const
{
    return m_isEditingFinished;
}

void gcmp::CoordinateInputDefinition::SetIsEditingFinished(bool isEditingFinished)
{
    m_isEditingFinished = isEditingFinished;
}

ILineEditEvent * gcmp::CoordinateInputDefinition::GetEvent() const
{ 
    return m_pLineEditDefinitionRefer->GetEvent(); 
}

void gcmp::CoordinateInputDefinition::SelectAll()
{
    m_pLineEditDefinitionRefer->SelectAll();
}

void gcmp::CoordinateInputDefinition::Clear()
{
    m_pLineEditDefinitionRefer->Clear();
}

std::wstring gcmp::CoordinateInputDefinition::GetSelectedText() const
{
    return m_pLineEditDefinitionRefer->GetSelectedText();
}

ControlDefinitionType gcmp::CoordinateInputDefinition::GetType() const
{
    return m_pLineEditDefinitionRefer->GetType();
}

std::wstring gcmp::CoordinateInputDefinition::GetId() const
{ 
    return m_pLineEditDefinitionRefer->GetId();
}

std::wstring gcmp::CoordinateInputDefinition::GetCaption() const 
{
    return m_pLineEditDefinitionRefer->GetCaption();
}

std::wstring gcmp::CoordinateInputDefinition::GetTooltip() const
{
    return m_pLineEditDefinitionRefer->GetTooltip();
}

bool gcmp::CoordinateInputDefinition::IsVisible() const 
{ 
    return m_pLineEditDefinitionRefer->IsVisible(); 
}

bool gcmp::CoordinateInputDefinition::IsEnabled() const 
{ 
    return m_pLineEditDefinitionRefer->IsEnabled();
}

bool gcmp::CoordinateInputDefinition::SetEnabled(bool enabled)
{
    return m_pLineEditDefinitionRefer->SetEnabled(enabled);
}

bool gcmp::CoordinateInputDefinition::SetVisible(bool visible)
{
    return m_pLineEditDefinitionRefer->SetVisible(visible);
}

bool gcmp::CoordinateInputDefinition::SetCoordinateInputEventHandler(ICoordinateInputEventHandler *opHandler)
{
    m_pEventHandler = opHandler;
    return true;
}

bool gcmp::CoordinateInputDefinition::SetLock(bool isLock)
{
    if (m_isLock != isLock)
    {
        m_isLock = isLock;
        if (m_pEventHandler)
        {
            CoordinateInputEventArgs args(CoordinateInputEventType::LockStateChange);
            args.SetLock(m_isLock);
            m_pEventHandler->On(&args);
        }
    }
    return true;
}

bool gcmp::CoordinateInputDefinition::IsLock() const
{
    return m_isLock;
}

//bool gcmp::CoordinateInputDefinition::SetStyleSheet(const std::wstring & styleSheet)
//{
//    if (m_styleSheet != styleSheet)
//    {
//        m_styleSheet = styleSheet;
//        if (m_pEventHandler)
//        {
//            CoordinateInputEventArgs args(CoordinateInputEventType::SetStyleSheet);
//            args.SetStyleSheet(styleSheet);
//            m_pEventHandler->On(&args);
//        }
//    }
//
//    return true;
//}
//
//std::wstring gcmp::CoordinateInputDefinition::GetStyleSheet() const
//{
//    return m_styleSheet;
//}

bool gcmp::CoordinateInputDefinition::SetValidator(OwnerPtr<ValidatorBase> pValidator)
{
    return m_pLineEditDefinitionRefer->SetValidator(TransferOwnership(pValidator));
}

ValidatorBase* gcmp::CoordinateInputDefinition::GetValidator()
{
    return m_pLineEditDefinitionRefer->GetValidator();
}

OwnerPtr<ILineEditDefinition> gcmp::CoordinateInputDefinition::GetDefinitionImpl()
{
    return TransferOwnership(m_opLineEditDefinition);
}
